/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Net.Server;

import java.io.*;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author Administrator
 */
public class netSocket {
    private Socket client ,temp=null;
    boolean isLive=true;
   InetAddress address;
   int port ;
   ServerSocket ss =null;
   DataOutputStream ps = null;
   DataInputStream inputStream = null;
   public netSocket(ServerSocket s) throws IOException{
       ss=s;
       client =s.accept();
       if (client == null)
            return;
       
        try {
            inputStream = new DataInputStream(new BufferedInputStream(client.getInputStream()));
        } catch (Exception e) {
            System.out.print("接收消息缓存错误\n");
            return;
        }
         ps = new DataOutputStream(client.getOutputStream());
   }
    private void sendMessage(){
      try{ 
        ps.writeByte(0x1);
               ps.flush();
      }catch(Exception e){}
    }
     public void downLoad() {
         reConnect("下载方法里面");
       

        try { ps.writeByte(0x1);
               ps.flush();
            //本地保存路径，文件名会自动从服务器端继承而来。
            String savePath ;
            int bufferSize = 8192;
            byte[] buf = new byte[bufferSize];
            int passedlen = 0;
            long len=0;
           
            savePath = inputStream.readUTF();System.out.println(savePath);
            DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath))));
            len = inputStream.readLong();
            
            System.out.println("文件的长度为:" + len + "\n");
            System.out.println("开始接收文件!" + "\n");
                    
            while (true) {
                int read = 0;
                if (inputStream != null) {
                    read = inputStream.read(buf);
                }
                passedlen += read;
                if (read == -1) {
                    break;
                }
                //下面进度条本为图形界面的prograssBar做的，这里如果是打文件，可能会重复打印出一些相同的百分比
                System.out.println("文件接收了" +  (passedlen * 100/ len) + "%\n");
                fileOut.write(buf, 0, read);
            }
            System.out.println("接收完成，文件存为" + savePath + "\n");

            fileOut.close();
            client.close();
            isLive =false ;
        } catch (Exception e) {
            System.out.println("接收消息错误" + "\n");
            return;
        }
    }
    public void  UpLoad(String filePath)throws Exception{
                reConnect("上传方法里面");
                File fi = new File(filePath);

                System.out.println("文件长度:" + (int) fi.length());
                //DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
                inputStream.readByte();// System.out.println("1");
               
               
               //System.out.println("2");
             
                DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
                //将文件名及长度传给客户端。这里要真正适用所有平台，例如中文名的处理，还需要加工，具体可以参见Think In Java 4th里有现成的代码。
                ps.writeUTF(fi.getName());
                ps.flush();
                ps.writeLong((long) fi.length());
                ps.flush();
 //System.out.println("3");
                int bufferSize = 8192;
                byte[] buf = new byte[bufferSize];

                while (true) {
                    int read = 0;
                    if (fis != null) {
                        read = fis.read(buf);
                    }

                    if (read == -1) {
                        break;
                    }
                    ps.write(buf, 0, read);
                }
                ps.flush();
                // 注意关闭socket链接哦，不然客户端会等待server的数据过来，
                // 直到socket超时，导致数据不完整。                
                fis.close();
                client.close();
                isLive = false ;
    }
    public   void write(String str) throws IOException {
        reConnect("写方法里面");
        ps.writeUTF(str);
    }
    public String read() throws IOException{
            reConnect("读方法里面");String message;
            message =inputStream.readUTF();
           return  message ;
}
    private void reConnect(String str){
    try{      if(!isLive){
                 client=ss.accept();  System.out.println("重新连接"+str);
            ps = new DataOutputStream(client.getOutputStream());
            inputStream = new DataInputStream(new BufferedInputStream(client.getInputStream()));
            isLive=true ;
            }
        } catch (Exception ex) {
           
        }
}
}
